package com.cha.chaApp.baby.controller;

import com.cha.chaApp.baby.service.BabyService;
import com.cha.chaCommon.core.common.R;
import com.cha.chaCommon.core.entity.Baby;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


@RestController
@Api(tags = "宝宝相册")
@RequestMapping("/baby")
public class UploadController {
    @Resource
    private BabyService babyService;

    @ApiOperation(
            value = "选择阶段，上传图片，并返回图片路径",
            notes = "根据上传的图片，返回图片路径")
    @PostMapping("/addAlbum")
    public R upload(@ApiParam(value = "图片，所选阶段", required = true, example = "")
                    MultipartFile file, Integer stage) {
        String path;
        try {
            path = babyService.upload(file.getInputStream(), file.getOriginalFilename(), stage);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok(path);
    }

    @ApiOperation(
            value = "添加宝宝信息",
            notes = "添加宝宝信息")
    @PostMapping("/add")
    public R addBaby(@ApiParam(value = "宝宝基本信息：性别，名字，出生日期", required = true, example = "男，小明，2021-01-01")
                     @RequestBody Baby baby) {
        babyService.addBaby(baby);
        return R.ok();
    }

    @ApiOperation(
            value = "删除图片",
            notes = "删除宝宝相册中的图片")
    @PostMapping("/delAlbum")
    public R delAblum(@ApiParam(value = "图片路径", required = true, example = "http://localhost:8080/baby/album/1.jpg")
                      String url) {
        babyService.delAlbum(url);
        return R.ok();
    }


    @ApiOperation(
            value = "修改宝宝信息",
            notes = "修改宝宝信息")
    @PostMapping("/changeBaby")
    public R changeBaby(@ApiParam(value = "修改宝宝信息", required = true, example = "男，小明，2021-01-01")
                        @RequestBody Baby baby) {
        babyService.updateBaby(baby);
        return R.ok();
    }

    @ApiOperation(
            value = "查询当前用户宝宝信息",
            notes = "查询宝宝信息")
    @GetMapping("/select")
    public R selectBaby() {
        List<Baby> babies = babyService.selectBaby();
        return R.ok(babies);

    }

    @ApiOperation(
            value = "查询当前用户宝宝相册",
            notes = "查询宝宝相册")
    @GetMapping("/selectAlbum")
    public R selectAlbum(@ApiParam(value = "所选阶段", required = true, example = "1")

                         Integer stage) {
        List<String> stringList = babyService.selectAlbum(stage);
        return R.ok(stringList);
    }
}
